//
//  TJTopUpController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/16.
//  Copyright © 2020 xtjun. All rights reserved.
//充币VC

#import "TJTopUpController.h"
#import "JXCategoryView.h"
#import "TJTopUpRecordController.h"

@interface TJTopUpController ()<JXCategoryViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (weak, nonatomic) UIImage *QRImage;
@property (weak, nonatomic) UIImageView *QRImageView;
@property (weak, nonatomic) UILabel *addressValueLabel;

/**btcWallet */
@property (copy, nonatomic) NSString *btcWallet;
/**usdtWallet */
@property (copy, nonatomic) NSString *usdtWallet;
/**切换链 */
@property (weak, nonatomic) UIButton *changeButton;
/**保存二维码 */
@property (weak, nonatomic) UIButton *saveButton;


@end

@implementation TJTopUpController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *titles = @[@"BTC", @"USDT"];
    
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithTitle:@"充币记录".localizedString color:UIColor.whiteColor target:self action:@selector(didClickNavBarRightButton)];
    
    self.navigationBar.backgroundColor = UIColor_Hex(0x2C6CC7);
    self.navigationBar.lineView.hidden = YES;
    [self.navigationBar backBarButtonWhiteColor];
    self.navigationBar.titleTextColor = UIColor.whiteColor;
    self.navigationBar.title = @"充币".localizedString;
    
    UIView *topView = [UIView tj_addToView:self.view];
    topView.backgroundColor = UIColor_Hex(0x2C6CC7);
    topView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, 180);
    
    self.view.backgroundColor = UIColor.whiteColor;
    self.categoryView.backgroundColor = UIColor.whiteColor;
    self.categoryView.layer.cornerRadius = 15;
    self.categoryView.layer.masksToBounds = YES;
    self.categoryView.titles = titles;
    self.categoryView.cellSpacing = 0;
    self.categoryView.delegate = self;
    self.categoryView.titleFont = UIFont_Px(14);
    self.categoryView.titleColor = UIColor_Hex(0x2C6CC7);
    self.categoryView.titleSelectedColor = [UIColor whiteColor];
    
    self.categoryView.titleLabelMaskEnabled = YES;
    self.categoryView.selectedAnimationEnabled = NO;
    self.categoryView.cellSpacing = 10;
    self.categoryView.cellWidth = 50;
    [topView addSubview:self.categoryView];
    self.categoryView.frame = CGRectMake((KScreenWidth - 130) * 0.5, 16, 130, 30);
    
    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
    backgroundView.indicatorHeight = 24;
    backgroundView.indicatorColor = UIColor_Hex(0x2C6CC7);
    self.categoryView.indicators = @[backgroundView];
    
    UIView *QRCodeView = [UIView tj_addToView:self.view];
    QRCodeView.backgroundColor = UIColor.whiteColor;
    [QRCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.equalTo(topView.mas_bottom).offset(20);
        make.height.width.offset(ZOOM(170));
    }];
    
    
    UIImageView *QRImageView = [UIImageView tj_addToView:QRCodeView];
    [QRImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(4);
        make.right.bottom.offset(-4);
    }];
    
    _QRImageView = QRImageView;
    
    //USDT的时候显示 切换链
    UIButton *changeButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(14) color:UIColor.tj_mainColor title:@"USDT-OMINI"];
    [changeButton setImage:[UIImage imageNamed:@"cb_change"] forState:UIControlStateNormal];
    [changeButton setImage:[UIImage imageNamed:@"cb_change"] forState:UIControlStateHighlighted];

    [changeButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
    [changeButton addTarget:self action:@selector(didClickChnageButton:) forControlEvents:UIControlEventTouchUpInside];
    [changeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(ZOOM(120));
           make.height.offset(ZOOM(35));
           make.centerX.offset(0);
           make.top.equalTo(QRImageView.mas_bottom).offset(12);
    }];
    changeButton.hidden = YES;
    self.changeButton = changeButton;
    
    UIButton *saveButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(14) color:UIColor.whiteColor title:@"保存二维码到相册".localizedString];
    saveButton.backgroundColor = UIColor_Hex(0x2C6CC7);
    [saveButton shearRoundedCornersWithRadiu:3];
    CGFloat saveButtonW = [saveButton.currentTitle getStringSizeWithWidth:200 font:UIFont_Px(14)].width + 30;
    [saveButton addTarget:self action:@selector(didClickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(saveButtonW);
        make.height.offset(30);
        make.centerX.offset(0);
        make.top.equalTo(QRImageView.mas_bottom).offset(12);
    }];
    self.saveButton = saveButton;
    
    UILabel *addressLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor_Hex(0x111111) title:@"充币地址".localizedString textAlignment:NSTextAlignmentLeft];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(saveButton.mas_bottom).offset(18);
        make.centerX.offset(0);
    }];
    
    UILabel *addressValueLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor_Hex(0x2C6CC7) title:@"" textAlignment:NSTextAlignmentCenter];
    addressValueLabel.numberOfLines = 0;
    [addressValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(addressLabel.mas_bottom).offset(8);
        make.centerX.offset(0);
        make.width.mas_lessThanOrEqualTo(KScreenWidth - 40);
    }];
    
    UIButton *copyButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(14) color:UIColor_Hex(0x2C6CC7) title:@"复制地址".localizedString];
    copyButton.backgroundColor = [UIColor_Hex(0x2C6CC7) colorWithAlphaComponent:0.2];
    [copyButton shearRoundedCornersWithRadiu:3];
    [copyButton addTarget:self action:@selector(didClickCopyButton) forControlEvents:UIControlEventTouchUpInside];
    [copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-100);
        make.left.offset(100);
        make.height.offset(30);
        make.centerX.offset(0);
        make.top.equalTo(addressLabel.mas_bottom).offset(50);
    }];
    
    
    _addressValueLabel = addressValueLabel;
    //    [self QRCodeContentStringWithCoinName:_coinTypes.firstObject coinNo:@"1"];
    
    //获取钱包地址
    [self loadWalletAddress];
    
    
}
/**切换链 */
- (void)didClickChnageButton:(UIButton *)btn {
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setTitle:@"USDT-ERC20" forState:UIControlStateNormal];
        [self createData: NO];
    }else{
         [btn setTitle:@"USDT-OMINI" forState:UIControlStateNormal];
        [self createData: YES];
    }
}

/**保存二维码 **/
- (void)didClickSaveButton {
    [Tools saveImageToPhotos:_QRImageView.image];
}
/**复制地址 **/
- (void)didClickCopyButton {
    [Tools copyStringToPasteboard:_addressValueLabel.text];
}

/**充币记录 **/
- (void)didClickNavBarRightButton {
    TJTopUpRecordController *topUpRecordController = [[TJTopUpRecordController alloc] init];
    
    topUpRecordController.title = @"充币记录".localizedString;
    topUpRecordController.type = 1;
    [self.navigationController pushViewController:topUpRecordController animated:YES];
}


- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {

    if (index == 0) {//BTC
        [self createData: YES];
        
        [self.saveButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_QRImageView.mas_bottom).offset(12);
        }];
        self.changeButton.hidden = YES;
    }else{//USDT
        [self.saveButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_QRImageView.mas_bottom).offset(12 + 42);
        }];
        self.changeButton.hidden = NO;
        if (self.changeButton.isSelected) {
            [self didClickChnageButton:self.changeButton];
        }


    }
    
}

- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
    }
    return _categoryView;
}


- (void)loadWalletAddress {
    //获取用户信息
    [TJNetworking.shareNetworking GET:@"member/balance/getWalletAddress" parameters:@{@"userId": TJUserInfo.readInfo.userId} success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        self.btcWallet =data[@"btcWallet"];// btc ||ustd/omini
        self.usdtWallet = data[@"ethWallet"]; //usdt/erc20
        
        [self createData: YES];
        
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

-(void)createData:(BOOL)isBTC
{
    NSString *address;
    if (isBTC) {// btc ||ustd/omini
        address = self.btcWallet;
    }else{//usdt/erc20
        address = self.usdtWallet;
    }
    self.addressValueLabel.text =  address;
    [Tools createQRWithString:address QRSize:CGSizeMake(ZOOM(165), ZOOM(165)) finishBlock:^(UIImage *image) {
        self.QRImageView.image = image;
        self.QRImage = image;
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end


